package vexe.web.thn.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vexe.web.thn.jdo.PARTNERS;
import vexe.web.thn.lib.BeanObject;
import vexe.web.thn.model.PartnersModel;

@SuppressWarnings("serial")
public class PartnersController extends Controller {

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(request.getQueryString()==null){
			PartnersModel model=new PartnersModel();
			request.setAttribute("PartnersList", model.getList());
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/View/partner.jsp");
			 dispatcher .forward(request, response);			 
			 return;
		}
		String action=request.getParameter("action");
		if(action.toUpperCase().equals("DELETE")){
			//response.sendError(404);
			PartnersModel model=new PartnersModel();
			model.Delete(request.getParameter("PARTNERID"));
			request.setAttribute("PartnersList", model.getList());
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/AjaxResult/PartnersList.jsp");
			 dispatcher .forward(request, response);
			 return;
		}
		if(action.toUpperCase().equals("REGISTER")){			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/View/PartnerDetail.jsp");
			 dispatcher .forward(request, response);
			 return;
		}
		if(action.toUpperCase().equals("EDIT")){
			PartnersModel model=new PartnersModel();
			request.setAttribute("Partner_obj", model.getPartners(request.getParameter("PARTNERID")));
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/View/PartnerDetail.jsp");
			 dispatcher .forward(request, response);
			 return;
		}
		
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub		
		String action=request.getParameter("action");
		if(action.toUpperCase().equals("ADD")){
			Add(request, response);
		}
	}
	public void Add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			PARTNERS partners=(PARTNERS) BeanObject.Bean(request,new PARTNERS());
			
			partners.setPARTNERID(request.getParameter("EMAILS"));
			/*partners.setEMAILS(request.getParameter("EMAILS"));
			partners.setPASSWORD(request.getParameter("PASSWORD"));
			partners.setADDRESS(request.getParameter("ADDRESS"));
			partners.setCITY(request.getParameter("CITY"));
			partners.setCOMPANYNAME(request.getParameter("COMPANYNAME"));
			partners.setNAMESTORE(request.getParameter("NAMESTORE"));
			partners.setMOBILEPHONE(request.getParameter("MOBILEPHONE"));
			partners.setPARTNERID(request.getParameter("EMAILS"));
			partners.setNAMES(request.getParameter("NAMES"));
			partners.setDATKB(1);
			partners.setPHONE(request.getParameter("PHONE"));
			partners.setPROVINCE(request.getParameter("PROVINCE"));*/
			PartnersModel model=new PartnersModel();
			model.Add(partners);
			response.sendRedirect("/Partner");
	}
}
